﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;

namespace GUI
{
    public partial class FormQLDLHoaDon : Form
    {
        public FormQLDLHoaDon()
        {
            InitializeComponent();
        }
        List<HOADONDTO> dsh;
        List<LOAIMONDTO> dsl;
        bool loai = false;
        private void DSHD(DateTime bd, DateTime kt)
        {
            dtgDanhSachHD.Rows.Clear();
            HOADONBUS HBUS = new HOADONBUS();
            dsh = new List<HOADONDTO>();
            dsh = HBUS.DSHD(bd, kt);
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Font = new Font(dtgDanhSachHD.Font, FontStyle.Bold);
            DataGridViewCellStyle style1 = new DataGridViewCellStyle();
            style1.Font = new Font(dtgDanhSachHD.Font, FontStyle.Strikeout);
            for (int i = 0; i < dsh.Count; i++)
            {
                //MaHD,Xoa,TrangThai,'BAN', 'TANG', 'THU', 'VAT'
                dtgDanhSachHD.Rows.Add(false, dsh[i].MaHD, dsh[i].Ban, dsh[i].Tang, dsh[i].Thu, dsh[i].VAT);
                if (dsh[i].TrangThai)//Đã thanh toán - In đậm
                {
                    dtgDanhSachHD.Rows[i].DefaultCellStyle = style;
                }
                else if (dsh[i].Xoa)//Bị xóa - In nghiêng
                {
                    dtgDanhSachHD.Rows[i].DefaultCellStyle = style1;
                }
            }
            DoanhThu(bd, kt);
        }
        private void DoanhThu(DateTime bd, DateTime kt)
        {
            dtgDoanhThu.Rows.Clear();
            HOADONBUS HBUS = new HOADONBUS();
            HOADONDTO dt = new HOADONDTO();
            dt = HBUS.DoanhThu(bd, kt);
            //'BAN', 'TANG', 'THU', 'VAT'
            if (dt.Ban == "Null")
                dt.Ban = "";
            if (dt.Tang == "Null")
                dt.Tang = "";
            if (dt.Thu == "Null")
                dt.Thu = "";
            if (dt.VAT == "Null")
                dt.VAT = "";
            dtgDoanhThu.Rows.Add(dt.Ban, dt.Tang, dt.Thu, dt.VAT);
        }
        private void DSLoai(DateTime bd, DateTime kt)
        {
            cboLoai.Items.Clear();
            dtgMon.Rows.Clear();
            dtgTongMon.Rows.Clear();
            LOAIMONBUS LBUS = new LOAIMONBUS();
            dsl = new List<LOAIMONDTO>();
            dsl = LBUS.DSLoai(bd,kt,loai);
            for (int i = 0; i < dsl.Count; i++)
            {
                cboLoai.Items.Add(dsl[i].TenLoai);
            }
            if (cboLoai.Items.Count > 0) cboLoai.SelectedIndex = 0;
        }
        private void DSMon(DateTime bd, DateTime kt)
        {
            dtgMon.Rows.Clear();
            THUCDONBUS TBUS = new THUCDONBUS();
            List<THUCDONDTO> dsm = new List<THUCDONDTO>();
            dsm = TBUS.DSMon(dsl[cboLoai.SelectedIndex], bd, kt);
            for (int i = 0; i < dsm.Count; i++)
            {
                //TenMon,DVT,'BAN', 'TANG', 'TONG'
                dtgMon.Rows.Add(dsm[i].TenMon, dsm[i].DVT, dsm[i].SL, dsm[i].KM, dsm[i].Tong);
            }
            TongMon(bd, kt);
        }
        private void TongMon(DateTime bd, DateTime kt)
        {
            dtgTongMon.Rows.Clear();
            THUCDONBUS TBUS = new THUCDONBUS();
            THUCDONDTO tm = new THUCDONDTO();
            tm = TBUS.TongMon(dsl[cboLoai.SelectedIndex], bd, kt);
            //'BAN', 'TANG', 'TONG'
            dtgTongMon.Rows.Add(tm.SL, tm.KM, tm.Tong);
        }
        private void FormQLDLHoaDon_Load(object sender, EventArgs e)
        {
            dtBatDau.Value = DateTime.Today;
            dtKetThuc.Value = DateTime.Now;
            DSHD(dtBatDau.Value, dtKetThuc.Value);
            DSLoai(dtBatDau.Value, dtKetThuc.Value);
        }
        private void chkTatCa_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dtgDanhSachHD.Rows.Count; i++)
            {
                dtgDanhSachHD.Rows[i].Cells[0].Value = chkTatCa.Checked;
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            HOADONBUS HBUS = new HOADONBUS();
            for (int i = 0; i < dtgDanhSachHD.Rows.Count; i++)
            {
                if (dsh[i].TrangThai == false && dsh[i].Xoa == false)
                {
                    continue;
                }
                else if (dtgDanhSachHD.Rows[i].Cells[0].Value.ToString() == "True")
                {
                    if (HBUS.XoaHD(dsh[i]) < 1)
                        MessageBox.Show("Xóa HĐ " + dsh[i].MaHD + " không thành công.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            chkTatCa.Checked = false;
            DSHD(dtBatDau.Value, dtKetThuc.Value);
            DSLoai(dtBatDau.Value, dtKetThuc.Value);
        }
        private void dtgDanhSachHD_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1) return;
            if(dsh[e.RowIndex].Tang=="Null")
                btnXemHD.Enabled=false;
            else
                btnXemHD.Enabled = true;
            //if (dsh[e.RowIndex].TrangThai==false && dsh[e.RowIndex].Xoa==false)//Khách đang ăn
            //    btnXoa.Enabled = false;
            //else
            //    btnXoa.Enabled = true;
        }
        private void cboLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoai.SelectedIndex == -1)
            {
                return;
            }
            DSMon(dtBatDau.Value, dtKetThuc.Value);
        }
        private void btnXemHD_Click(object sender, EventArgs e)
        {
            HOADONBUS HBUS = new HOADONBUS();
            HOADONDTO HDTO = new HOADONDTO();
            ////Xem hóa đơn khi đã gọi món
            HDTO.MaHD = dsh[dtgDanhSachHD.CurrentCell.RowIndex].MaHD;
            FormHoaDon frm = new FormHoaDon();
            frm.HDTO = new HOADONDTO();
            frm.HDTO.MaHD = HDTO.MaHD;
            frm.HDTO.ThoiGian = HBUS.ThoiGianThanhToan(HDTO).ThoiGian;
            frm.ShowDialog();
        }
        private void dtKetThuc_ValueChanged(object sender, EventArgs e)
        {
            DSHD(dtBatDau.Value, dtKetThuc.Value);
            DSLoai(dtBatDau.Value, dtKetThuc.Value);
        }
        private void dtBatDau_ValueChanged(object sender, EventArgs e)
        {
            DSHD(dtBatDau.Value, dtKetThuc.Value);
            DSLoai(dtBatDau.Value, dtKetThuc.Value);
        }
        private void dtgDanhSachHD_SelectionChanged(object sender, EventArgs e)
        {
            int r = dtgDanhSachHD.CurrentCell.RowIndex;
            if (r == -1) return;
            if (dsh[r].Tang == "Null")
                btnXemHD.Enabled = false;
            else
                btnXemHD.Enabled = true;
            //if (dsh[r].TrangThai == false && dsh[r].Xoa == false)//Khách đang ăn
            //    btnXoa.Enabled = false;
            //else
            //    btnXoa.Enabled = true;
        }
        private void rdNuoc_CheckedChanged(object sender, EventArgs e)
        {
            if (rdNuoc.Checked)
            {
                loai = true;
            }
            else
            {
                loai = false;
            }
            DSLoai(dtBatDau.Value, dtKetThuc.Value);
        }
    }
}
